package cn.xfdzx.android.apps.shop.presenter;

import cn.xfdzx.android.apps.shop.base.BasePresenter;
import cn.xfdzx.android.apps.shop.bean.LicenseImgBeen;
import cn.xfdzx.android.apps.shop.bean.LiveListBeen;
import cn.xfdzx.android.apps.shop.bean.OneBeen;
import cn.xfdzx.android.apps.shop.contract.LiveListContract;
import cn.xfdzx.android.apps.shop.contract.ShopInContract;
import cn.xfdzx.android.apps.shop.model.LiveListModel;
import cn.xfdzx.android.apps.shop.model.ShopInModel;
import cn.xfdzx.android.apps.shop.net.RxScheduler;
import io.reactivex.functions.Consumer;

/**
 * @author azheng
 * @date 2018/6/4.
 * Description：
 */
public class LiveListPresenter extends BasePresenter<LiveListContract.View> implements LiveListContract.Presenter {

    private LiveListContract.Model model;

    public LiveListPresenter() {
        model = new LiveListModel();
    }


    @Override
    public void getLiveList(boolean isRefresh, boolean isLoading,int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getLiveList(page)
                .compose(RxScheduler.<LiveListBeen>Flo_io_main())
                .as(mView.<LiveListBeen>bindAutoDispose())
                .subscribe(new Consumer<LiveListBeen>() {
                    @Override
                    public void accept(LiveListBeen bean) throws Exception {
                        mView.onLiveListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }

    @Override
    public void getLiveFocusList(boolean isRefresh, boolean isLoading, int page) {
        //View是否绑定 如果没有绑定，就不执行网络请求
        if (!isViewAttached()) {
            return;
        }
        if (isLoading){
            mView.showLoading();
        }
        model.getLiveFocusList(page)
                .compose(RxScheduler.<LiveListBeen>Flo_io_main())
                .as(mView.<LiveListBeen>bindAutoDispose())
                .subscribe(new Consumer<LiveListBeen>() {
                    @Override
                    public void accept(LiveListBeen bean) throws Exception {
                        mView.onLiveFocusListSuccess(bean);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.onError(throwable);
                        if (isLoading){
                            mView.hideLoading();
                        }
                        if (isRefresh){
                            mView.finishRefresh();
                        }else {
                            mView.finishLoadMore();
                        }
                    }
                });
    }
}
